package Store;
import java.io.File;

import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

/**
 * 
 * @author Xenophon
 * This is the interface of storing and retrieval. The Database
 * we used is Berkeley DB Java Edition.
 *
 */
public class StoreDB {
	private String dataHome;
	private String CLASS_CATALOG = "java_class_catalog";
	private String ID_Database   = "id_database";
	
	private Environment env;
	private Database    idInfoDb;
	private StoredClassCatalog javaClassCatalog;
	
	public StoreDB(String home_base)
	{
		this.dataHome = home_base;
		//Open The db in transactional mode...
		System.out.println("Open database Environment in Directory:"+dataHome);
		EnvironmentConfig env_config = new EnvironmentConfig();
		env_config.setAllowCreate(true);
		env_config.setTransactional(true);
		env = new Environment(new File(dataHome), env_config);
		
		DatabaseConfig d_config = new DatabaseConfig();
		d_config.setAllowCreate(true);
		d_config.setTransactional(true);
		//Now we create the class log database;
		Database class_catalog = env.openDatabase(null, CLASS_CATALOG, d_config);
		javaClassCatalog = new StoredClassCatalog(class_catalog);
		
		idInfoDb = env.openDatabase(null, ID_Database, d_config);
	}
	
	public Database GetIDDatabase()
	{
		return idInfoDb;
	}
	
	public Environment GetEnvironment()
	{
		return env;
	}
	
	public StoredClassCatalog GetClassLog()
	{
		return javaClassCatalog;
	}

	public void Close()
	{
		idInfoDb.close();
		javaClassCatalog.close();
		env.close();
	}
}
